一般情况下Swit要想调用obj-c,c或c++代码必须通过obj-c以及桥接文件才可以办到,但是使用
@_silgen_name
,可以对于某些简单的代码,直接跳过桥接文件和.h头文件与C代码交互。
创建.c文件
1 | #include <stdio.h> |
创建.swift文件,引用.c文件函数
1 | // 在全局作用域下进行声明 |
例:
Swift 引用 “@_silgen_name”
@_silgen_name(“yudpsocket_server”) func c_yudpsocketserver( host:UnsafePointer
@_silgen_name(“yudpsocket_recive”) func c_yudpsocketrecive( fd:Int32,buff:UnsafePointer
@_silgen_name(“yudpsocket_close”) func c_yudpsocketclose( fd:Int32) -> Int32
@_silgen_name(“yudpsocket_client”) func c_yudpsocket_client() -> Int32
@_silgen_name(“yudpsocket_get_server_ip”) func c_yudpsocket_get_serverip( host:UnsafePointer
@_silgen_name(“yudpsocket_sentto”) func c_yudpsocketsentto( fd:Int32,buff:UnsafePointer
@_silgen_name(“enable_broadcast”) func c_enablebroadcast( fd:Int32)
///////yudpsocket.c
#include
#include
#include
#include
#include
#include
#include
#include
#define yudpsocket_buff_len 8192
//return socket fd
int yudpsocket_server(const char *address, int port) {
//create socket
int socketfd=socket(AF_INET, SOCK_DGRAM, 0);
int reuseon = 1;
int r = -1;
//bind
struct sockaddr_in serv_addr;
memset( &serv_addr, ‘\0’, sizeof(serv_addr));
serv_addr.sin_len = sizeof(struct sockaddr_in);
serv_addr.sin_family = AF_INET;
if (address == NULL || strlen(address) == 0 || strcmp(address, “255.255.255.255”) == 0) {
r = setsockopt(socketfd, SOL_SOCKET, SO_BROADCAST, &reuseon, sizeof(reuseon));
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
} else {
r = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon));
serv_addr.sin_addr.s_addr = inet_addr(address);
serv_addr.sin_port = htons(port);
}
if (r == -1) {
return -1;
}
r = bind(socketfd, (struct sockaddr ) &serv_addr, sizeof(serv_addr));
if (r == 0) {
return socketfd;
} else {
return -1;
}
}
int yudpsocket_recive(int socket_fd, char outdata, int expted_len, char remoteip, int remoteport) {
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
memset(&cli_addr, 0x0, sizeof(struct sockaddr_in));
int len = (int)recvfrom(socket_fd, outdata, expted_len, 0, (struct sockaddr )&cli_addr, &clilen);
char clientip = inet_ntoa(cli_addr.sin_addr);
memcpy(remoteip, clientip, strlen(clientip));
remoteport = cli_addr.sin_port;
return len;
}
int yudpsocket_close(int socket_fd) {
return close(socket_fd);
}
//return socket fd
int yudpsocket_client() {
//create socket
int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
int reuseon = 1;
setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon));
return socketfd;
}
//enable broadcast
void enable_broadcast(int socket_fd) {
int reuseon = 1;
setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &reuseon, sizeof(reuseon));
}
int yudpsocket_get_server_ip(char host, char ip) {
struct hostent hp;
struct sockaddr_in address;
hp = gethostbyname(host);
if (hp == NULL) {
return -1;
}
bcopy((char )hp->h_addr, (char )&address.sin_addr, hp->h_length);
char clientip = inet_ntoa(address.sin_addr);
memcpy(ip, clientip, strlen(clientip));
return 0;
}
//send message to address and port
int yudpsocket_sentto(int socket_fd, char msg, int len, char toaddr, int topotr) {
struct sockaddr_in address;
socklen_t addrlen = sizeof(address);
memset(&address, 0x0, sizeof(struct sockaddr_in));
address.sin_family = AF_INET;
address.sin_port = htons(topotr);
address.sin_addr.s_addr = inet_addr(toaddr);
int sendlen = (int)sendto(socket_fd, msg, len, 0, (struct sockaddr )&address, addrlen);
return sendlen;
}